<?php

/**
* AdminShowGuideTest.php
*
* Guid add test
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once '../framework/config.php';

require_once 'simpletest/autorun.php';
require_once 'simpletest/web_tester.php';

require_once '../framework/AuthSingleton.php';



class  AdminShowGuideTest extends WebTestCase
{
    private $testName,$testDesc,$url;
    private $username = '';
    private $password = '';


    public function setUp() 
    {
        $this->username = md5( uniqid( rand(), true ) )."@pl.pl";
        $this->password = "zuza";

        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->url = $global['service_url'];
        $this->testName = md5(uniqid(rand(), true));
        $this->testDesc = md5(uniqid(rand(), true));
        $this->addHeader("User-Agent: Mozilla/5.0(SymbianOS/9.2; U; Series60/3.1 NokiaE51/1.00.000; Profile/MIDP-2.0 Configuration/CLDC-1.1;)AppleWebKit/413(KHTML,like Gecko)Safari/413"); 
        $this->get($this->url.'?cmd=AdminGuideList');
        AuthSingleton::instance()->addUser($this->username,$this->password);
        $this->setFieldByName('username', $this->username);
        $this->setFieldByName('password', $this->password);
        $this->click("Login");


    }

    public function testShowTextOnly()
    {
        $this->get($this->url.'?cmd=AdminAddGuide');
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->click("Zapisz");
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $sth = $dbh->prepare("SELECT ROWID FROM guide WHERE name = \"".$this->testName."\" AND desc = \"".$this->testDesc."\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$result[0]);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertField('pic_file'); 
        $this->assertField('sound_file');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) );
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 1 ) );         
        
    }

    public function testShowTextAndPic()
    {
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $this->get($this->url.'?cmd=AdminAddGuide');
        $this->assertTitle('Wirtualny Przewodnik Administracja');

        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/images.jpg'));
        $this->assertFieldByName('pic_file', dirname(__FILE__).'/images.jpg');

        $this->click("Zapisz");
    
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $sth = $dbh->prepare("SELECT ROWID FROM guide WHERE name = \"".$this->testName."\" AND desc = \"".$this->testDesc."\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$result[0]);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertText($result[0].'images.jpg'); 
        $this->assertField('pic_file'); 
        $this->assertField('delete_pic_file'); 
        $this->assertField('sound_file');
        $this->assertFalse( $this->setFieldByName("delete_sound_file", 1 ) ); 
        $this->assertTrue(is_file($upload['upload_dir'].$result[0].'images.jpg')); 

    }

    public function testShowTextAndSound()
    {
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');

        $this->get($this->url.'?cmd=AdminAddGuide');
        $this->assertTitle('Wirtualny Przewodnik Administracja');

        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/sound.wav'));
        $this->assertFieldByName('sound_file', dirname(__FILE__).'/sound.wav');

        $this->click("Zapisz");
    
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       
        $sth = $dbh->prepare("SELECT ROWID FROM guide WHERE name = \"".$this->testName."\" AND desc = \"".$this->testDesc."\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$result[0]);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertLink('Pobierz dzwięk',$global['service_url'].'send_sound.php?id='.$result[0].'sound.wav'); 
        $this->assertField('sound_file'); 
        $this->assertField('delete_sound_file'); 
        $this->assertField('sound_file');
        $this->assertFalse( $this->setFieldByName("delete_pic_file", 1 ) ); 
        $this->assertTrue(is_file($upload['upload_dir'].$result[0].'sound.wav')); 

    }

    public function testShowAll()
    {
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
        $this->get($this->url.'?cmd=AdminAddGuide');
        $this->assertTitle('Wirtualny Przewodnik Administracja');

        $this->setFieldByName("name", $this->testName);
        $this->setFieldByName("desc", $this->testDesc);
        $this->assertTrue($this->setFieldByName('pic_file', dirname(__FILE__).'/images.jpg'));
        $this->assertFieldByName('pic_file', dirname(__FILE__).'/images.jpg');
        $this->assertTrue($this->setFieldByName('sound_file', dirname(__FILE__).'/sound.wav'));
        $this->assertFieldByName('sound_file', dirname(__FILE__).'/sound.wav');

        $this->click("Zapisz");

        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $dbh = new PDO($options['dsn_string']); 
        $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );       

        $sth = $dbh->prepare("SELECT ROWID FROM guide WHERE name = \"".$this->testName."\" AND desc = \"".$this->testDesc."\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$result[0]);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
            $this->assertLink('Pobierz dzwięk',$global['service_url'].'send_sound.php?id='.$result[0].'sound.wav'); 
        $this->assertField('sound_file'); 
        $this->assertField('delete_sound_file'); 
        $this->assertField('sound_file');
        $this->assertTrue(is_file($upload['upload_dir'].$result[0].'sound.wav')); 

        $sth = $dbh->prepare("SELECT ROWID FROM guide WHERE name = \"".$this->testName."\" AND desc = \"".$this->testDesc."\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_BOTH);        
        $this->get($this->url.'?cmd=AdminAddGuide&id='.$result[0]);
        $this->assertTitle('Wirtualny Przewodnik Administracja');
        $this->assertField('name', $this->testName);
        $this->assertField('desc', $this->testDesc);
        $this->assertText($result[0].'images.jpg'); 
        $this->assertField('pic_file'); 
        $this->assertField('delete_pic_file'); 
        $this->assertField('sound_file');
        $this->assertTrue(is_file($upload['upload_dir'].$result[0].'images.jpg')); 

    }

    public function tearDown() 
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','DSN');  
        $upload = &PEAR::getStaticProperty('CONFIG_INI','UPLOAD');
  
        $dbh = new PDO($options['dsn_string']);        
        $sth = $dbh->prepare("SELECT pic_file,sound_file FROM guide WHERE name = \"".$this->testName."\" AND desc = \"".$this->testDesc."\"");
        $sth->execute();
        $result = $sth->fetch(PDO::FETCH_ASSOC);
        
        
        if ( is_file($upload['upload_dir'].$result['pic_file']) )
        {
            unlink($upload['upload_dir'].$result['pic_file']);
        }

        if ( is_file($upload['upload_dir'].$result['sound_file']) )
        {
            unlink($upload['upload_dir'].$result['sound_file']);
        }

        $sth = $dbh->prepare("DELETE FROM guide WHERE name = \"".$this->testName."\" AND desc = \"".$this->testDesc."\"");
        $sth->execute();

        AuthSingleton::instance()->removeUser($this->username);
        $this->get($this->url.'?cmd=AdminGuideList&logout=1');
        
    }
    
}
?>
